package com.smfx.yxt.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smfx.yxt.common.Const;
import com.smfx.yxt.daoSqlserver.OrganizeDepartMapper;
import com.smfx.yxt.daoSqlserver.OrganizeGroupMapper;
import com.smfx.yxt.daoSqlserver.TeacherInfoMapper;
import com.smfx.yxt.model.OrganizeDepart;
import com.smfx.yxt.model.OrganizeGroup;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.model.TeacherOrganize;
import com.smfx.yxt.model.TeacherOrganizeGroup;
import com.smfx.yxt.service.IOrganizeDepartService;
import com.smfx.yxt.utils.StringUtil;
import com.smfx.yxt.view.TermView;
import com.smfx.yxt.wechat.utils.WechatGlobalMap;

@Service("organizeDepartService")
public class OrganizeDepartServiceImpl implements IOrganizeDepartService {

	@Autowired
	private OrganizeDepartMapper organizeDepartMapper;
	@Autowired
	private TeacherInfoMapper teacherInfoMapper;
	@Autowired
	private OrganizeGroupMapper organizeGroupMapper;
	
	@Override
	public List<OrganizeDepart> selectListByParam(OrganizeDepart record) {
		return organizeDepartMapper.selectListByParam(record);
	}

	@Override
	public List<OrganizeDepart> selectComboBox() {
		return organizeDepartMapper.selectComboBox();
	}

	@Override
	public List<TeacherOrganizeGroup> fillTeacherOrganizeGroup(Integer schoolId) {
		List<OrganizeGroup> list = organizeGroupMapper.selectListByParam(new OrganizeGroup());
		return getOrganizeGroup(list, schoolId);
	}
	
	@Override
	public List<TeacherOrganize> fillTeacherOrganize(Integer schoolId) {
		List<OrganizeDepart> list = organizeDepartMapper.selectListByParam(new OrganizeDepart());
		Map<String, List<TeacherOrganize>> postionMap = new HashMap<String, List<TeacherOrganize>>();
		Map<Integer, Integer> postionDepartMap = new HashMap<>();
		List<TeacherOrganize> departList = new ArrayList<>();
		if(list != null && list.size() > 0) {
			for(OrganizeDepart menu : list) {
				String key = null;
				Integer parentId = menu.getParentId();
				if(!(parentId != null && parentId != 0)) {
					key = "root";
				}else {
					key = String.valueOf(parentId);
				}
				List<TeacherOrganize> childMenuList = postionMap.get(key);
				if(childMenuList == null) {
					childMenuList = new ArrayList<TeacherOrganize>();
				}
				TeacherOrganize teacherOrganize = new TeacherOrganize();
				if(key.equals("root")) {
					teacherOrganize.setDepartId(menu.getId());
					teacherOrganize.setDepart(menu.getDepart());
					departList.add(teacherOrganize);
				}else {
					teacherOrganize.setDepartId(menu.getParentId());
					teacherOrganize.setPositionId(menu.getId());
					teacherOrganize.setPosition(menu.getPosition());
					postionDepartMap.put(menu.getId(), menu.getParentId());
				}
				childMenuList.add(teacherOrganize);
				postionMap.put(key, childMenuList);
			}
			List<TeacherInfo> teacherList = teacherInfoMapper.selectAll(schoolId);
			if(teacherList != null && teacherList.size() > 0) {
				for(TeacherInfo teacher : teacherList) {
					if(StringUtils.isBlank(teacher.getPosition())){
						continue;
					}
					String[] array = teacher.getPosition().split(",");
					for(String positionId : array) {
						List<TeacherOrganize> childMenuList = postionMap.get(positionId);
						if(childMenuList == null) {
							childMenuList = new ArrayList<TeacherOrganize>();
						}
						TeacherOrganize teacherOrganize = new TeacherOrganize();
						teacherOrganize.setPositionId(Integer.valueOf(positionId));
						teacherOrganize.setTeacherId(teacher.getId());
						teacherOrganize.setTeacherName(teacher.getName());
						teacherOrganize.setDepartId(postionDepartMap.get(Integer.valueOf(positionId)));
						childMenuList.add(teacherOrganize);
						postionMap.put(positionId, childMenuList);
					}
					
				}
			}
			for(TeacherOrganize menu : departList) {
				menu.setSubList(postionMap.get(String.valueOf(menu.getDepartId())));
				List<TeacherOrganize> list2 = menu.getSubList();
				if(!StringUtil.isNullOrEmpty(menu.getSubList())) {
					menu.setHasChild(true);
				}
				if(list2 != null) {
					for(TeacherOrganize teacher : list2) {
						teacher.setSubList(postionMap.get(String.valueOf(teacher.getPositionId())));
					}
				}
			}
			postionMap = null;
			return departList;
		}
		return null;
	}

	@Override
	public List<TeacherOrganizeGroup> fillTeacherOrganizeGroupByGroupIds(String groupIds, Integer schoolId) {
		List<String> groupIdList = Arrays.asList(groupIds.split(","));
		List<OrganizeGroup> list = organizeGroupMapper.selectListByGroupIds(groupIdList);
		return getOrganizeGroup(list, schoolId);
	}
	
	@Override
	public List<OrganizeDepart> selectGroupComboBox(OrganizeGroup organizeGroup) {
		return organizeDepartMapper.selectGroupComboBox(organizeGroup);
	}

	private List<TeacherOrganizeGroup> getOrganizeGroup(List<OrganizeGroup> list, Integer schoolId) {
		Map<String, List<TeacherOrganizeGroup>> postionMap = new HashMap<String, List<TeacherOrganizeGroup>>();
		List<TeacherOrganizeGroup> departList = new ArrayList<>();
		List<TeacherOrganizeGroup> secondList = new ArrayList<>();
		if(list != null && list.size() > 0) {
			for(OrganizeGroup menu : list) {
				String key = null;
				Integer parentId = menu.getParentId();
				if(!(parentId != null && parentId != 0)) {
					key = "root";
				}else {
					key = String.valueOf(parentId);
				}
				List<TeacherOrganizeGroup> childMenuList = postionMap.get(key);
				if(childMenuList == null) {
					childMenuList = new ArrayList<TeacherOrganizeGroup>();
				}
				TeacherOrganizeGroup teacherOrganize = new TeacherOrganizeGroup();
				if(key.equals("root")) {
					teacherOrganize.setParentGroupId(menu.getParentId());
					teacherOrganize.setGroupId(menu.getId());
					teacherOrganize.setGroupName(menu.getGroupName());
					teacherOrganize.setIds(menu.getIds());
					departList.add(teacherOrganize);
				}else {
					teacherOrganize.setGroupId(menu.getId());
					teacherOrganize.setGroupName(menu.getGroupName());
					teacherOrganize.setParentGroupId(menu.getParentId());
					teacherOrganize.setIds(menu.getIds());
					teacherOrganize.setGroupCode(menu.getGroupCode());
				}
				if(StringUtils.isNotBlank(menu.getIds())) {
					secondList.add(teacherOrganize);
				}
				childMenuList.add(teacherOrganize);
				postionMap.put(key, childMenuList);
			}
			List<TeacherInfo> teacherList = teacherInfoMapper.selectAll(schoolId);
			if(teacherList != null && teacherList.size() > 0) {
				for(TeacherInfo teacher : teacherList) {
					for(TeacherOrganizeGroup second : secondList) {
						if(StringUtils.isNotBlank(second.getGroupCode())) {
							List<TeacherOrganizeGroup> childMenuList = postionMap.get(String.valueOf(second.getGroupId()));
							if(childMenuList == null) {
								childMenuList = new ArrayList<TeacherOrganizeGroup>();
							}
							if(second.getGroupCode().equals("SUBJECT_GROUP")) {
								if(StringUtil.isNotNullOrZero(teacher.getSubjectId()) && ("," + second.getIds() + ",").indexOf(("," + teacher.getSubjectId() + ",")) != -1) {
									TeacherOrganizeGroup teacherOrganize = new TeacherOrganizeGroup();
									teacherOrganize.setTeacherId(teacher.getId());
									teacherOrganize.setTeacherName(teacher.getName());
									teacherOrganize.setGroupId(second.getGroupId());
									teacherOrganize.setParentGroupId(second.getParentGroupId());
									childMenuList.add(teacherOrganize);
									postionMap.put(String.valueOf(second.getGroupId()), childMenuList);
								}
							}else if(second.getGroupCode().equals("GRADE_GROUP")) {
								if(teacher.getGrade() != null) {
									TermView view = (TermView) WechatGlobalMap.get(Const.CACHE_TERM).getValue();
									int currGrade = view.getFinishYear()+7-(Integer.valueOf(second.getIds()));
									if(teacher.getGrade().intValue() == currGrade) {
										TeacherOrganizeGroup teacherOrganize = new TeacherOrganizeGroup();
										teacherOrganize.setTeacherId(teacher.getId());
										teacherOrganize.setTeacherName(teacher.getName());
										teacherOrganize.setGroupId(second.getGroupId());
										teacherOrganize.setParentGroupId(second.getParentGroupId());
										childMenuList.add(teacherOrganize);
										postionMap.put(String.valueOf(second.getGroupId()), childMenuList);
									}
								}
							}
						}
					}
					if(StringUtils.isBlank(teacher.getPosition())){
						continue;
					}
					String[] array = teacher.getPosition().split(",");
					for(String positionId : array) {
						for(TeacherOrganizeGroup second : secondList) {
							if(("," + second.getIds() + ",").indexOf(("," + positionId + ",")) != -1) {
								List<TeacherOrganizeGroup> childMenuList = postionMap.get(String.valueOf(second.getGroupId()));
								if(childMenuList == null) {
									childMenuList = new ArrayList<TeacherOrganizeGroup>();
								}
								TeacherOrganizeGroup teacherOrganize = new TeacherOrganizeGroup();
								teacherOrganize.setTeacherId(teacher.getId());
								teacherOrganize.setTeacherName(teacher.getName());
								teacherOrganize.setIds(teacher.getPosition());
								teacherOrganize.setGroupId(second.getGroupId());
								teacherOrganize.setParentGroupId(second.getParentGroupId());
								childMenuList.add(teacherOrganize);
								postionMap.put(String.valueOf(second.getGroupId()), childMenuList);
							}
						}
					}
					
				}
			}
			for(TeacherOrganizeGroup menu : departList) {
				List<TeacherOrganizeGroup> groupList = postionMap.get(String.valueOf(menu.getGroupId()));
				if(!StringUtil.isNullOrEmpty(groupList)) {
					menu.setHasChild(true);
					removeDuplicateWithOrder(groupList);
					menu.setSubList(groupList);
				}
				List<TeacherOrganizeGroup> list2 = menu.getSubList();
				if(list2 != null) {
					for(TeacherOrganizeGroup teacher : list2) {
						if(teacher.getTeacherId() == null) {
							groupList = postionMap.get(String.valueOf(teacher.getGroupId()));
							teacher.setSubList(groupList);
						}
					}
				}
			}
			postionMap = null;
			return departList;
		}
		return null;
	}
	
	public static void removeDuplicateWithOrder(List<TeacherOrganizeGroup> list) {    
	   Map<Integer, TeacherOrganizeGroup> map = new HashMap<>();
	   List<TeacherOrganizeGroup> newList = new ArrayList<>();
	   for(TeacherOrganizeGroup group : list) {
		   if(!StringUtil.isNotNullOrZero(group.getTeacherId())) {
			   newList.add(group);
			   continue;
		   }
		   if(!map.containsKey(group.getTeacherId())) {
			   newList.add(group);
			   map.put(group.getTeacherId(), group);
		   }
	   }
	   list.clear();
	   map.clear();
	   list.addAll(newList);    
	 }  
}
